package com.zwh.rabbitmq.module.project.mq;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.Payload;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.zwh.rabbitmq.module.project.ProjectMqConstants;
import com.zwh.rabbitmq.module.project.mq.ProjectInfoDto;

import lombok.extern.slf4j.Slf4j;

/**
 * 项目rabbitmq消息监听
 * 
 * @author zwh
 * @date 2021/12/7 17:58
 **/
@Configuration
@Slf4j
public class ProjectRabbitMqListener {
    /**
     * 项目是否能删除redis缓存key
     */
    private final String PROJECT_CAN_DEL_KEY = "project_can_delete";

    /**
     * 项目是否能删除的监听事件 使用redis存合同是否能删除的最后更新时间，如果小于更新时间的消息，直接返回
     *
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = ProjectMqConstants.PROJECT_DELETE_QUEUE, durable = "true", autoDelete = "false"),
        exchange = @Exchange(value = ProjectMqConstants.CONTRACT_OR_PROJECT_EXCHANGE, type = ExchangeTypes.DIRECT),
        key = ProjectMqConstants.PROJECT_DELETE_QUEUE))
    public void projectCanDelete(@Payload ProjectInfoDto data, Message message, Channel channel)
        throws IOException {
        try {
            // 处理数据
            log.info("接收mq消息：{}", JSONObject.toJSONString(data));

        } catch (Exception e) {
            log.error("Exception异常：{}", e.getMessage());
            log.error("数据：{}", JSONObject.toJSONString(data));
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            return;
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
